package com.sdry.work.service;

import com.sdry.system.entity.SysUser;
import com.sdry.untils.DateUtil;
import com.sdry.untils.FileUtil;
import com.sdry.work.entity.Contract;
import com.sdry.work.entity.WorkPlan;
import com.sdry.work.mapper.WorkMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author lian
 * @date 2020-04-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkServiceImpl implements WorkService {

    @Resource
    private WorkMapper workMapper;

    /**
     * 根据条件分页查询工作计划
     * @param workPlan 工作计划实体类
     * @return 返回null，没有查到
     */
    @Override
    public List<WorkPlan> queryWorkPlan(WorkPlan workPlan) {
        return workMapper.queryWorkPlan(workPlan);
    }

    /**
     * 根据条件，查询工作计划的列表的行数
     * @param workPlan 工作计划实体类
     * @return 返回0，没查到
     */
    @Override
    public Long countWorkPlan(WorkPlan workPlan) {
        return workMapper.countWorkPlan(workPlan);
    }

    /**
     * 给工作计划表插入数据
     * @param workPlan 工作计划实体类
     * @return 返回插入行的ID，返回0，插入失败
     */
    @Override
    public Long insertWorkPlan(WorkPlan workPlan) {
        String nowTime = DateUtil.dateFormat2();
        workPlan.setCreateTime(nowTime);
        return workMapper.insertWorkPlan(workPlan);
    }

    /**
     * 根据ID修改工作计划表插入数据
     * @param workPlan 工作计划实体类
     * @return 返回影响行数，返回0，修改失败
     */
    @Override
    public Long updateWorkPlan(WorkPlan workPlan) {
        return workMapper.updateWorkPlan(workPlan);
    }

    /**
     * 根据ID删除工作计划表插入数据
     * @param ids 工作计划ID拼接的字符串
     * @return 返回影响行数，返回0，删除失败
     */
    @Override
    public Long deleteWorkPlan(String ids) {
        return workMapper.deleteWorkPlan(ids);
    }

    /**
     * 根据条件分页查询合同
     * @param contract 合同实体类
     * @return 返回null，没有查到
     */
    @Override
    public List<Contract> queryContract(Contract contract) {
        return workMapper.queryContract(contract);
    }

    /**
     * 根据条件，查询合同的列表的行数
     * @param contract 合同实体类
     * @return 返回0，没查到
     */
    @Override
    public Long countContract(Contract contract) {
        return workMapper.countContract(contract);
    }

    /**
     * 给合同表插入数据
     * @param param 合同实体类
     * @return 返回插入行的ID，返回0，插入失败
     */
    @Override
    public Long insertContract(Contract param) {
        String nowTime = DateUtil.dateFormat2();
        param.setCreatedDate(nowTime);
        return workMapper.insertContract(param);
    }

    /**
     * 根据ID修改合同表插入数据
     * @param param 合同实体类
     * @return 返回影响行数，返回0，修改失败
     */
    @Override
    public Long updateContract(Contract param) {
        return workMapper.updateContract(param);
    }

    /**
     * 根据ID删除合同表插入数据
     * @param ids 合同ID拼接的字符串
     * @return 返回影响行数，返回0，删除失败
     */
    @Override
    public Long deleteContract(String ids) {
        return workMapper.deleteContract(ids);
    }

    /**
     * 图片上传方法
     * @param file  图片名称
     * @param request   请求
     * @return  返回成功，失败
     */
    @Override
    public Map uploadImg(MultipartFile file, HttpServletRequest request) {
        return FileUtil.upload(file, 0);
    }

    /**
     * 查询所有未删除的用户
     * @return 返回null，没查到数据
     */
    @Override
    public List<SysUser> getAllUser() {
        return workMapper.getAllUser();
    }

    /**
     * 文件上传方法
     * @param file  文件名称
     * @param request   请求
     * @return  返回成功，失败
     */
    @Override
    public Map uploadFile(MultipartFile file, HttpServletRequest request) {
        return FileUtil.upload(file, 1);
    }

    /**
     * 根据条件分页查询待审批工作计划
     * @param workPlan 工作计划实体类
     * @return 返回null，没有查到
     */
    @Override
    public List<WorkPlan> queryApprovalWorkPlan(WorkPlan workPlan) {
        return workMapper.queryApprovalWorkPlan(workPlan);
    }

    /**
     * 根据条件，查询待审批工作计划的行数
     * @param workPlan 工作计划实体类
     * @return 返回0，没有查到
     */
    @Override
    public Long countApprovalWorkPlan(WorkPlan workPlan) {
        return workMapper.countApprovalWorkPlan(workPlan);
    }

    /**
     * 根据ID，修改工作计划状态
     * @param workPlan 工作计划实体类
     * @return 返回0，修改失败
     */
    @Override
    public Long updateWorkPlanStatus(WorkPlan workPlan) {
        String notTime = DateUtil.dateFormat3();
        workPlan.setApprovalTime(notTime);
        return workMapper.updateWorkPlanStatus(workPlan);
    }

    /**
     * 根据条件分页查询待审批合同
     * @param contract 合同实体类
     * @return 返回null，没有查到
     */
    @Override
    public List<Contract> queryApprovalContract(Contract contract) {
        return workMapper.queryApprovalContract(contract);
    }

    /**
     * 根据条件，查询待审批合同的行数
     * @param contract 合同实体类
     * @return 返回0，没有查到
     */
    @Override
    public Long countApprovalContract(Contract contract) {
        return workMapper.countApprovalContract(contract);
    }

    /**
     * 根据ID修改合同状态
     * @param param 实体条件
     * @return 返回0，修改失败
     */
    @Override
    public Long updateContractStatus(Contract param) {
        String notTime = DateUtil.dateFormat3();
        param.setApprovalTime(notTime);
        return workMapper.updateContractStatus(param);
    }

}
